package com.example.method3;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @program: springboot_course
 * @description:
 * @author: ChenZhiXiang
 * @create: 2019-08-06 20:36
 **/
@RestController
public class DemoMain {

    /**
     * 4. 通过PropertiesLoaderUtils获取(注意，此工具类仅可处理.properties或.xml配置文件)
     */
    public Object getAttrByPropertiesLoaderUtils() {
        try {
            ClassPathResource resource = new ClassPathResource("application.properties");
            Properties properties = PropertiesLoaderUtils.loadProperties(resource);
            String property = properties.getProperty("czx", "UNDEFINED");
            System.out.println("通过PropertiesLoaderUtils获取: " + property);
            return property;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/d")
    public Object c(){
        Map<String,Object> map = new HashMap<>();
        map.put("child1",getAttrByPropertiesLoaderUtils());
        return map;
    }
}
